$(function () {

    // 移除购物车
    $(".first_eight").on("click",function () {
        axios.get("/car/carDel/"+$(this).attr("cid")).then(res => {
            if(res.data.code == 200){
                console.log("200");
                $(this).parent().remove();
            }else{
                console.log(500);
            }
        });

    });
    // 计算总金额
    function countMoney(){
        var $sumDv = $(".sum,.sum2");
        var $checkNum = $(".float_count");
        var sum = 0;
        var countCheck = 0;
        $.each($(".first_conent .cart-checkbox label").filter(".check"),function (index,element) {
            countCheck ++;
             sum += parseFloat($(element).parent().parent().siblings(".first_seven").children("span").html());
            console.log(sum);
        });
        sum = toDecimal2(sum);
        $sumDv.html(sum);
        $checkNum.html(countCheck);
    }
    var $float_jiesuan =$(".float_jiesuan");
    var $check = $(".first_conent .cart-checkbox label")
    function selectedAll() {

        if($check.filter(".check").length != $check.length){
            $(".title .cart-checkbox label,.float_hezi label").removeClass("check");
        }else{
            $(".title .cart-checkbox label,.float_hezi label").addClass("check");
        }
        if($check .filter(".check").length != 0){
            $float_jiesuan.addClass("float_jiesuan_select").addClass("float_jiesuan_select:hover")
        }else{
            if($float_jiesuan.hasClass("float_jiesuan_select")){
                $float_jiesuan.removeClass("float_jiesuan_select").removeClass("float_jiesuan_select:hover")
            }
        }
    }

    function toDecimal2(x) {
        var f = parseFloat(x);
        if (isNaN(f)) {
            return false;
        }
        var f = Math.round(x*100)/100;
        var s = f.toString();
        var rs = s.indexOf('.');
        if (rs < 0) {
            rs = s.length;
            s += '.';
        }
        while (s.length <= rs + 2) {
            s += '0';
        }
        return s;
    }
    var len = $(".first_conent").length;
    $(".txt2").html(len);
    $(".first_conent").eq(len-1).css("margin-bottom","60px");
    $(window).on("scroll",function () {
        // 判断滚动条是否到底
        if($(document).scrollTop() >= $(document).height()-$(window).height()){
            if($(".float_bar").hasClass("fixed_bar")){
                $(".float_bar").addClass("static_bar").removeClass("fixed_bar");
            }
        }else{
            if($(".float_bar").hasClass("static_bar")){
                $(".float_bar").removeClass("static_bar").addClass("fixed_bar");
            }
        }
    });
    // 对物品数量的操作
    $(".first_six_a2").on("click",function () {
        var $txt = $(this).siblings("input");
        var num = parseInt($txt.val());
        num+=1;
        $txt.val(num);
        if($txt.val() >= 1 && $(".first_six_a1").hasClass("banBtn")){
            $(".first_six_a1").css({"cursor":"pointer","color":"#f40"});
            $(".first_six_a1").removeClass("banBtn")
        }
        // 通过
        var price = parseFloat($(this).parents().find(".first_five_p2 span").html());
        var count = price * num;
        count = toDecimal2(count);
        console.log($(this).parent().parent().siblings(".first_seven").children("span").html(count));
        countMoney();
    });

    $(".first_six_a1").on("click",function () {
        if(!$(this).hasClass("banBtn")){
            var $txt = $(this).siblings("input");
            var num = parseInt($txt.val());
            if(num <= 1 ){
                $(".first_six_a1").css("cursor","not-allowed").css("color","#ccc");
                $(this).addClass("banBtn");
            }else{
                num -= 1;
                $txt.val(num);
                var price = parseFloat($(this).parents().find(".first_five_p2 span").html());
                var count = price * num;
                count = toDecimal2(count);
                console.log($(this).parent().parent().siblings(".first_seven").children("span").html(count));
                countMoney();
            }
        }
    });
    // check
    $(".first_conent .cart-checkbox label").on("click",function () {
        $(this).toggleClass("check");
        countMoney();
        selectedAll();
    });
    $(".title .cart-checkbox label,.float_hezi label").on("click",function () {
        $(".title .cart-checkbox label").toggleClass("check");
        $(".float_hezi label").toggleClass("check");
        if($(this).hasClass("check")){
            $(".first_conent .cart-checkbox label").addClass("check");
        }else{
            $(".first_conent .cart-checkbox label").removeClass("check");
        }
        countMoney();
        selectedAll();
    });
    // 提交金额
    $(".float_jiesuan").on("click",function () {
        // 判断是否选择购买的商品
        if($float_jiesuan.hasClass("float_jiesuan_select")){
            var count = $check.filter(".check").length;
            var ids = [];
            var nums = [];
            for(var i = 0 ; i < count ; i++ ){
                ids.push($check.filter(".check").eq(i).attr("for"));
                nums.push($check.filter(".check").eq(i).parent().parent().siblings(".first_six").children(".first_six_div1").children(".first_six_input").val());
            }
            $.ajax({
                async: "false",
                type:"POST",
                url:"/car/clearCar",
                data:{ids:ids,nums:nums},
                success:function (data) {
                    if(data.code == 200){
                        window.location.href="/pay/check/"+data.tid;
                    }
                },
                dataType:"json"
            })

            /*$.post("/car/clearCar",
                {ids:ids,nums:nums},
                function (data) {

                },"json");*/

        }});
});